## Replication file for "The Policy Effects of the Partisan Composition of State Government"
## Authors: Devin Caughey, Chris Warshaw, Yiqing Xu

## Plotting figures based on panel regression results


## 1. Dynamic Effects
## 2. Baseline Estimates
## 3. Asscess Model Fit: The Case of California
## 4. Disentangling Share and Control

## install.packages("foreign")
## install.packages("ggplot2")
## install.packages("doBy")

library(foreign)
rm(list=ls(all=TRUE))
#setwd("~/Dropbox/Projects/PartyControl_StateLegislatures/Replication_Archive")
**Set the working directory in R to whatever directory you downloaded the replication materials from our paper.

###################################
## A5: Dynamic Effect
###################################

library(foreign)
(d<-read.dta("fg_dyn.dta"))
(d <- rbind(d[1:4,],rep(0,6),d[5:9,]))

pdf("PanelDynamic.pdf",height=9)
par(mfcol=c(3,1),mar=c(4,5,2.5,1))
## governor
plot(1,xlim=c(-4,5),ylim=c(-0.10,0.10),xlab="",ylab="",axes=F)
box()
abline(h = seq(-0.1,0.1,0.05), col = "gray80", lwd=0.5)
abline(v = seq(-4,5,1), col = "gray80", lwd=0.5)
abline(h=0,lwd=1,lty=3)
abline(v=0,col="gray",lwd=2,lty=2)
points(x0,d[,1],pch=16,col=1,cex=1.5)
for (i in 1:10) {
    lines(c(x0[i],x0[i]),c(d[i,1]-1.96*d[i,4],d[i,1]+1.96*d[i,4]),col=1,lwd=2)
}
axis(1,cex.axis=1.1,at=x0)
par(las=1);axis(2,cex.axis=1.2,lwd=0,lwd.ticks = 1)
par(las=0);mtext("Coefficient",side=2,line=3.5,cex=0.8)
mtext("Year(s) after Election",side=1,line=2,cex=0.8)
title("Dem. Governor",line=1)
## house
plot(1,xlim=c(-4,5),ylim=c(-0.10,0.10),xlab="",ylab="",axes=F)
box()
abline(h = seq(-0.1,0.1,0.05), col = "gray80", lwd=0.5)
abline(v = seq(-4,5,1), col = "gray80", lwd=0.5)
abline(h=0,lwd=1,lty=3)
abline(v=0,col="gray",lwd=2,lty=2)
points(x0,d[,2],pch=16,col=1,cex=1.5)
for (i in 1:10) lines(c(x0[i],x0[i]),c(d[i,2]-1.96*d[i,5],d[i,2]+1.96*d[i,5]),col=1,lwd=2)
axis(1,cex.axis=1.1,at=x0)
par(las=1);axis(2,cex.axis=1.2,lwd=0,lwd.ticks = 1)
par(las=0);mtext("Coefficient",side=2,line=3.5,cex=0.8)
mtext("Year(s) after Election",side=1,line=2,cex=0.8)
title("Dem. House Majority",line=1)
## senate
plot(1,xlim=c(-4,5),ylim=c(-0.10,0.10),xlab="",ylab="",axes=F)
box()
abline(h = seq(-0.1,0.1,0.05), col = "gray80", lwd=0.5)
abline(v = seq(-4,5,1), col = "gray80", lwd=0.5)
abline(h=0,col="gray",lwd=2)
abline(v=0,col="gray",lwd=2,lty=2)
points(x0,d[,3],pch=16,col=1,cex=1.5)
for (i in 1:10) lines(c(x0[i],x0[i]),c(d[i,3]-1.96*d[i,6],d[i,3]+1.96*d[i,6]),col=1,lwd=2)
axis(1,cex.axis=1.1,at=x0)
par(las=1);axis(2,cex.axis=1.2,lwd=0,lwd.ticks = 1)
par(las=0);mtext("Coefficient",side=2,line=3.5,cex=0.8)
mtext("Year(s) after ELection",side=1,line=2,cex=0.8)
title("Dem. Senate Majority",line=1)
graphics.off()



###################################
## A7: Baseline Estimates
###################################

(d<-read.dta("fg_baseline.dta"))
d <- rbind(c(0, 0, 0, 0), d)
d$x0 <- c(0.93, 1.93, 2.93, 3.93, 1.08, 2.08, 3.08, 4.08)
d$governor <- c(rep("Otherwise",4), rep("Democratic Governor", 4))

library(ggplot2)
p <- ggplot(data = d) +
    geom_hline(yintercept = 0, colour="gray80", size = 1.5, linetype = "dashed") +
    geom_point(aes(x = x0, y = coef, color = governor, shape = governor), size = 4) +
    geom_segment(aes(x = x0, xend = x0, y = CI1, yend = CI2,
                     color = governor), size = 1.2) +
    theme_bw() + coord_cartesian(ylim = c(-0.04, 0.12), xlim = c(0.8, 4.2)) +
    ylab("Coefficient") + xlab("Democratic Majority in Legislatures") +
    theme(legend.position = c(0.5, 0.95), legend.direction = "horizontal",
          legend.title = element_blank(), legend.key = element_rect(colour=NA),
          legend.key.width = unit(1,"cm"), text = element_text(size=10),
          legend.background = element_rect(fill = NA)) +
    scale_colour_manual(values = c("black", "gray60"))  +
    scale_x_continuous(breaks = c(1, 2, 3, 4),
                       labels = c("Neither","House","Senate","Both"))

pdf("PanelBaseline.pdf",width=7,height=3.5)
print(p)
graphics.off()


###############################################
## A8: Assessing Model Fit: The Case of California
###############################################

library(foreign)
d<-read.dta("fg_modelfit.dta")
s<-d[which(d$abb=="CA"),]
s$year<-s$year+1936

pdf("PanelModelfitCA.pdf",width=12,height=7)
par(mar=c(3,3,1,1))
plot(1,type="n",xlim=c(1936,2014),ylim=c(0.5,3),xlab="",ylab="",axes=F)
lines(s$year, s$Policy_fe,col="gray50",lwd=3,lty=2)
lines(s$year, s$Policy_trd3,col="4",lwd=4,lty=3)
lines(s$year, s$Policy_lg2,col="2",lwd=3,lty=4)
lines(s$year, s$Policy,lwd=3)
mtext("Year",1,2)
mtext("Policy Liberalsim",2,2)
axis(2)
axis(1,at=c(seq(1936,2014,by=4),2014),cex=0.5)
text(1962,1.75,"Twoway FEs",col="gray50")
text(2011,2.80,"FEs + Cubic",col=4)
text(2011,2.25,"FEs + LDV",col=2)
text(1996,2.4,"Actual",col=1)
graphics.off()

